using UnityEngine;
using System.Threading.Tasks;

public class AsyncLoadTextAsset : MonoBehaviour
{
    public string textAssetName;

    private async void Start()
    {
        textAssetName = "NewFile1";
        await LoadTextAssetAsync(textAssetName);
    }

    void Update()
    {
        // 输出时间
        Debug.Log("Time.time");
    }
    
    private async Task LoadTextAssetAsync(string assetName)
    {
        // 异步加载 TextAsset
        ResourceRequest request = Resources.LoadAsync<TextAsset>(assetName);
        // 等待加载完成
        await WaitForRequestCompletion(request);

        TextAsset textAsset = request.asset as TextAsset;
        if (textAsset != null)
        {
            Debug.Log("TextAsset 加载完成：" + textAsset.text);
        }
        else
        {
            Debug.LogError("未找到指定的 TextAsset：" + assetName);
        }
    }

    private async Task WaitForRequestCompletion(ResourceRequest request)
    {
        while (!request.isDone)
        {
            await Task.Yield();
        }
    }
}